
package com.be.beadmin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.service.dto.input.PrintDesignFileDto;
import com.be.beadmin.service.dto.output.PrintDesignFileEx;
import com.be.beadmin.service.entity.PrintDesignFile;
import com.be.beadmin.service.entity.PrintDesignFile_;
import com.be.beadmin.service.mapper.PrintDesignFileMapper;
import com.be.beadmin.service.mapper.PrintDesignFileMapperEx;
import com.be.beadmin.service.repository.PrintDesignFileRepository;
import com.be.beadmin.service.service.PrintDesignFileService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author chenwei
* @date 2023-05-10
**/
@Service
@RequiredArgsConstructor
public class PrintDesignFileServiceImpl implements PrintDesignFileService {

    @Autowired
    private  PrintDesignFileRepository printDesignFileRepository;
    @Autowired
    private  PrintDesignFileMapper printDesignFileMapper;
    @Autowired
    private  PrintDesignFileMapperEx printDesignFileExMapper;

    @Override
    public Page<PrintDesignFileEx> getPrintDesignFileByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<PrintDesignFile> page = printDesignFileRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(PrintDesignFile_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(PrintDesignFile_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<PrintDesignFileEx> page1 = page.map(printDesignFileExMapper::toEntity);
            return page1;

    }

    @Override
    public void addPrintDesignFile(PrintDesignFileDto printDesignFileDto) {
            PrintDesignFile result = printDesignFileRepository.findPrintDesignFileByIdAndDelete(printDesignFileDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("PrintDesignFile不存在,请重新查询");
            }
            PrintDesignFile printDesignFile = printDesignFileMapper.toEntity(printDesignFileDto);
            printDesignFileRepository.save(printDesignFile);
    }

    @Override
    public void updatePrintDesignFile(PrintDesignFileDto printDesignFileDto) {
            PrintDesignFile printDesignFile = printDesignFileRepository.findPrintDesignFileById(printDesignFileDto.getId());
            printDesignFile = printDesignFileMapper.toEntity(printDesignFileDto);
            printDesignFileRepository.save(printDesignFile);
    }


    @Override
    public void delPrintDesignFile(String id) {
        PrintDesignFile printDesignFile = printDesignFileRepository.findPrintDesignFileById(id);
        printDesignFile.setDelete(1);
        printDesignFileRepository.save(printDesignFile);
    }

    @Override
    public void delPrintDesignFiles (List<String> ids) { }

    @Override
    public void updateFileContent(PrintDesignFileDto printDesignFileDto) {
        PrintDesignFile printDesignFile = printDesignFileRepository.findPrintDesignFileById(printDesignFileDto.getId());
        if(ObjectUtil.isEmpty(printDesignFile)) {
            throw new BeadminException("没有设计模板主信息");
        }
        printDesignFile.setFileContext(printDesignFileDto.getFileContext());
        printDesignFileRepository.save(printDesignFile);
    }
}